package com.oa.ylms.apps.actions.assess;

import java.io.IOException;
import java.util.List;

import net.sf.json.JSONObject;

import com.google.gson.Gson;
import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.assess.IAssessItemService;
import com.oa.ylms.apps.business.assess.IAssessModelService;
import com.oa.ylms.apps.business.assess.IItemCategoryService;
import com.oa.ylms.apps.entity.assess.AssessItemValue;
import com.oa.ylms.apps.entity.assess.AssessModelValue;
import com.oa.ylms.apps.entity.assess.ItemCategoryValue;

@SuppressWarnings("serial")
public class AssessModelAction extends BasicAction {

	private IAssessModelService assessModelService;
	private IAssessItemService assessItemService;
	private IItemCategoryService itemCategoryService;
	private AssessModelValue assessModel;
	private List<AssessModelValue> list;
	private List<AssessItemValue> assessItemList;
	private List<ItemCategoryValue> categoryList;

	private int itemId;
	private int modelId;
	private int categoryId;
	private String modelName;
	private String modelIds;
	private String result;
	private int add = 0;

	public String query() {
		try {
			list = assessModelService.queryAssessModelWithPage(modelName, rows,
					currpage);
			totalpage = assessModelService.countTpages(modelName, rows);
			count = assessModelService.getTotalNumber();
			if (count % rows == 0 && totalpage + 1 == currpage) {
				currpage = currpage - 1;
				totalpage = currpage;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "show";
	}

	public String queryDetail() {
		try {
			assessModel = assessModelService.queryAssessModelByModelId(modelId);
			assessItemList = assessItemService
					.queryAssessItemByItemIds(assessModel.getItemIds());
		} catch (Exception e) {
			e.printStackTrace();
		}
		Gson gson = new Gson();
		session.put("assessItemList", assessItemList);
		String str = gson.toJson(session);
		try {
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().println(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String manager() {
		try {
			totalpage = assessModelService.countTpages(modelName, rows);
			count = assessModelService.getTotalNumber();
			if (count % rows == 0 && totalpage + 1 == currpage) {
				currpage = currpage - 1;
				totalpage = currpage;
			}
			if (add == 1) {
				currpage = totalpage;
			}
			list = assessModelService.queryAssessModelWithPage(modelName, rows,
					currpage);
			categoryList = itemCategoryService.queryAllItemCategory();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "manager";
	}

	public String toEnableAssessModel() {
		try {
			assessModelService.toEnable(modelId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toEnableAssessModel";

	}

	public String toDisableAssessModel() {
		try {
			assessModelService.toDisable(modelId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toDisableAssessModel";

	}

	public String addModel() throws Exception {
		JSONObject json = JSONObject.fromObject(result);
		String modelName = json.getString("assessModelName");
		String itemIds = json.getString("itemIds");
		if (modelName.length() != 0 && itemIds.length() != 0) {
			itemIds = itemIds.substring(0, itemIds.length() - 1);
			assessModelService.addAssessModel(modelName, itemIds);
		}
		return null;
	}

	public String deleteAssessModel() {
		try {
			assessModelService.deleteAssessModel(modelId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "delete";
	}

	public String deleteMoreAssessModel() {
		try {
			assessModelService.deleteAssessModel(modelIds);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "delete";
	}

	public String getItemsByCategoryId() throws Exception {
		assessItemList = assessItemService
				.queryAssessItemByCategoryId(categoryId);
		Gson obj = new Gson();
		String str = obj.toJson(assessItemList);
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().write(str);
		return null;
	}

	public IAssessModelService getAssessModelService() {
		return assessModelService;
	}

	public void setAssessModelService(IAssessModelService assessModelService) {
		this.assessModelService = assessModelService;
	}

	public AssessModelValue getAssessModel() {
		return assessModel;
	}

	public void setAssessModel(AssessModelValue assessModel) {
		this.assessModel = assessModel;
	}

	public String getModelName() {
		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

	public List<AssessModelValue> getList() {
		return list;
	}

	public void setList(List<AssessModelValue> list) {
		this.list = list;
	}

	public int getModelId() {
		return modelId;
	}

	public void setModelId(int modelId) {
		this.modelId = modelId;
	}

	public List<AssessItemValue> getAssessItemList() {
		return assessItemList;
	}

	public void setAssessItemList(List<AssessItemValue> assessItemList) {
		this.assessItemList = assessItemList;
	}

	public IAssessItemService getAssessItemService() {
		return assessItemService;
	}

	public void setAssessItemService(IAssessItemService assessItemService) {
		this.assessItemService = assessItemService;
	}

	public int getItemId() {
		return itemId;
	}

	public void setItemId(int itemId) {
		this.itemId = itemId;
	}

	public String getModelIds() {
		return modelIds;
	}

	public void setModelIds(String modelIds) {
		this.modelIds = modelIds;
	}

	public int getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(int categoryId) {
		this.categoryId = categoryId;
	}

	public IItemCategoryService getItemCategoryService() {
		return itemCategoryService;
	}

	public void setItemCategoryService(IItemCategoryService itemCategoryService) {
		this.itemCategoryService = itemCategoryService;
	}

	public List<ItemCategoryValue> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<ItemCategoryValue> categoryList) {
		this.categoryList = categoryList;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public int getAdd() {
		return add;
	}

	public void setAdd(int add) {
		this.add = add;
	}
}
